package com.tyrone.concurrent.basic;


import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 优先级
 * @author xieyl
 * @create 2021/11/30 21:07
 */
public class Priority {
    public static volatile boolean notStart = true;
    public static volatile boolean notEnd = true;

    public static void main(String[] args) throws InterruptedException {
        List<Job> jobs = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            int priority = i < 5 ? Thread.MIN_PRIORITY : Thread.MAX_PRIORITY;
            Job job = new Job(priority);
            jobs.add(job);
            Thread thread = new Thread(job, "Thread" + i);
            thread.setPriority(priority);
            thread.start();
        }
        notStart = false;
        //主线程休眠10秒，让其余是十个线程有充分时间执行
        TimeUnit.SECONDS.sleep(10);
        //停止这十个线程
        notEnd = false;
        for (Job job : jobs) {
            System.out.println("Job Priority :" + job.priority+"count:" + job.jobCount);
        }
    }
    static class Job implements Runnable{
        private int priority ;
        private long jobCount;
        public Job(int priority){
            this.priority = priority ;
        }
        @Override
        public void run (){
            //启动时，通过Thread.yield 阻塞在这里  等待十个线程全部启动之后，在跑
            while (notStart){
                Thread.yield();
            }
            while(notEnd){
                Thread.yield();
                jobCount++;
            }
        }
    }
}
